/**
 *  <Java Class>
 *
 *   Copyright (C) 2011  Fernando Gil
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   The following additional terms will apply to the software provieded with
 *   this file.
 *      1. Preserve the original authors’ names.
 *      2. Documenting all contributions and specify the contributor's name,
 *         the date of the contribution and some email to future contact.
 *
 *   @author  Fernando Gil
 *   @mail    lobho.gil@gmail.com
 *   @company Development Solutions (DS) Software
 *   @date    17-11-2011
 *   @modify  17-11-2011
 *   @by      lobho
 *   @see     License GNU GPL v3  (/lic/GNU_GPLv3_EN)
 *   @see     Additional Terms    Point 7.1 on License (/lic/GNU_GPLv3_EN)
 *   @note   Replicate this header in all classes.
 *
 */
package org.dss.gil.util;

/**
 *
 * @author francisco.gil
 */
public class StringFunctions {

    /** Remove all the ocurrences of the <code>character</code>
     *  found in the <code>string</code>
     * @param string is the String where to search
     * @param character is the char to remove
     * @return the new String without character ocurrences
     */
    public String remove(String string, char character) {
        String newString = "";
        char[] characters = string.toCharArray();
        for (char c : characters) {
            if (c != character) {
                newString+= c + "";
            }
        }
        return newString;
    }

    /** Count the total count of charcters found in the string
     *  who are = <code>character</code>
     * @param string
     * @param character
     * @return
     */
    public int counter(String string, char character) {
        int counter = 0;
        char[] characters = string.toCharArray();
        for (char c : characters) {
            if (c == character) {
                counter++;
            }
        }
        return counter;
    }

    public int counter(String string, String secuence) {
        int counter = 0;
        int index = 0;
        int length = string.length();
        int seclength = secuence.length();
        while (index + seclength <= length) {
            if (string.substring(index, index + seclength).equals(secuence)) {
                counter++;
            }
            index++;
        }
        return counter;
    }

    private int counterIgnoringInQuotes(String string, String secuence) {
        int counter = 0;
        int index = 0;
        int length = string.length();
        int seclength = secuence.length();
        boolean inQuotes = false;
        while (index + seclength <= length) {
            if(string.substring(index,index+1).equals("\""))
                inQuotes = (inQuotes)?false:true;
            if (string.substring(index, index + seclength).equals(secuence)) {
                if(!inQuotes)
                    counter++;
            }
            index++;
        }
        return counter;
    }
    /** Separeta the Text as token elements, with the delimiter.
     *  Every token in a separeated array position.
     *  Example:
     *   Text= A-"B-C"-D
     *   Delimiter = -
     *   Result = [A,"B,C",D]
     * @param text
     * @param delimiter
     * @return
     */
    public String[] getTokens(String text, String delimiter) {
        String[] tokens = new String[counter(text, delimiter) + 1];   //Create the array who will contain tokens
        int pos = 0;                                                    //Current position in array
        int length = text.length();                                     //Text's length
        int dellength = delimiter.length();                             //Delimiter's length
        int index = 0;                                                  //Char position on Text
        if(text.contains(delimiter)){
            while (pos < tokens.length) {                                       //For each array position. Create the token
                String token = "";                                          //The token always is "" in the begining
                while (index + dellength <= length) {
                    String sub = text.substring(index, index + dellength);    //Obtain a substring with the samen length than the delimiter
                    if (sub.equals(delimiter)) {                            //compare with delimiter, if is a delimiter
                        index += dellength;                                    //increment the index and ignore the substring
                        break;                                               //Break the while
                    } else {                                                  //if the substring is not a delimiter
                        token += text.substring(index, index + 1);             //save the character in the index position
                        index++;                                             //increment index
                    }
                }
                tokens[pos++] = token;          //Save the Token created in the previous while
            }
            /** Sometimes we lost some character after the last delimeter, so we need to retrieve them*/
            if (index < length) {
                tokens[tokens.length - 1] = text.substring(text.lastIndexOf(delimiter) + dellength);
            }
        }else
            tokens[0] = text;
        return tokens;
    }

    /** Separate the Text as token elements, with the delimiter.
     *  Every token in a separeated array position. When the
     *  boolean is true the method will ignore all the delimeters
     *  who are into " "
     *  Example:
     *   Text= A-"B-C"-D
     *   Delimiter = -
     *   Result = [A,"B-C",D]
     * 
     * @param text
     * @param delimiter
     * @param ignore
     * @return
     */
    public String[] getTokens(String text, String delimiter, boolean ignore) {
        if(!ignore)
            return this.getTokens(text, delimiter);
        else{
            boolean inQuotes=false;
            String[] tokens = new String[counterIgnoringInQuotes(text, delimiter) + 1];   //Create the array who will contain tokens
            int pos = 0;                                                    //Current position in array
            int length = text.length();                                     //Text's length
            int dellength = delimiter.length();                             //Delimiter's length
            int index = 0;                                                  //Char position on Text
            if(text.contains(delimiter)){
                while (pos < tokens.length) {                                       //For each array position. Create the token
                    String token = "";                                          //The token always is "" in the begining
                    while (index + dellength <= length) {
                        if(text.substring(index,index+1).equals("\""))
                            inQuotes = (inQuotes)?false:true;

                        String sub = text.substring(index, index + dellength);    //Obtain a substring with the samen length than the delimiter
                        if (sub.equals(delimiter) && !inQuotes) {                 //compare with delimiter, if is a delimiter
                            index += dellength;                                    //increment the index and ignore the substring
                            break;                                               //Break the while
                        } else {                                                  //if the substring is not a delimiter
                            token += text.substring(index, index + 1);             //save the character in the index position
                            index++;                                             //increment index
                        }
                    }
                    tokens[pos++] = token;          //Save the Token created in the previous while
                }
                /** Sometimes we lost some character after the last delimeter, so we need to retrieve them*/
                if (index < length) {
                    tokens[tokens.length - 1] = text.substring(text.lastIndexOf(delimiter) + dellength);
                }
            }else
                tokens[0] = text;
            return tokens;
        }
    }

    /** This function return the same array pased in the param
     *  with the trim() method aplied to every value.
     * @param array
     * @return
     */
    public String[] trimStringArray(String [] array){
        int index=0;
        while(index<array.length)
            array[index]=array[index++].trim();
        return array;
    }

    public String[] toStringArray(String s){
        char ch[] = s.toCharArray();
        String st [] = new String[ch.length];
        int i=0;
        for(char c :ch)
            st [i]=ch[i++]+"";
        return st;
    }

}
